/**
 * 数据导入管理初始化
 */
var ExcelUpload = {
    id: "ExcelUploadTable",	//表格id
    seItem: null,		//选中的条目
    table: null,
    layerIndex: -1
};

/**
 * 初始化表格的列
 */
ExcelUpload.initColumn = function () {
    return [
        {field: 'selectItem', radio: true},
        {title: '主键id', field: 'id', visible: true, align: 'center', valign: 'middle'},
        {
            title: '文件名', field: 'filename', visible: true, align: 'center', valign: 'middle', formatter(value, row, index){
                return '<a href="/static/'+value+'">'+value+'</a>'
            }
        },
        {title: '上传时间', field: 'uploadtime', visible: true, align: 'center', valign: 'middle'}
    ];
};

/**
 * 检查是否选中
 */
ExcelUpload.check = function () {
    var selected = $('#' + this.id).bootstrapTable('getSelections');
    if(selected.length == 0){
        Feng.info("请先选中表格中的某一记录！");
        return false;
    }else{
        ExcelUpload.seItem = selected[0];
        return true;
    }
};

/**
 * 点击添加数据导入
 */
ExcelUpload.openAddExcelUpload = function () {
    var index = layer.open({
        type: 2,
        title: '添加数据导入',
        area: ['800px', '420px'], //宽高
        fix: false, //不固定
        maxmin: true,
        content: Feng.ctxPath + '/excelUpload/excelUpload_add'
    });
    this.layerIndex = index;
};

/**
 * 打开查看数据导入详情
 */
ExcelUpload.openExcelUploadDetail = function () {
    if (this.check()) {
        var index = layer.open({
            type: 2,
            title: '数据导入详情',
            area: ['800px', '420px'], //宽高
            fix: false, //不固定
            maxmin: true,
            content: Feng.ctxPath + '/excelUpload/excelUpload_update/' + ExcelUpload.seItem.id
        });
        this.layerIndex = index;
    }
};

/**
 * 删除数据导入
 */
ExcelUpload.delete = function () {
    if (this.check()) {
        var ajax = new $ax(Feng.ctxPath + "/excelUpload/delete", function (data) {
            Feng.success("删除成功!");
            ExcelUpload.table.refresh();
        }, function (data) {
            Feng.error("删除失败!" + data.responseJSON.message + "!");
        });
        ajax.set("excelUploadId",this.seItem.id);
        ajax.start();
    }
};

/**
 * 查询数据导入列表
 */
ExcelUpload.search = function () {
    var queryData = {};
    queryData['condition'] = $("#condition").val();
    ExcelUpload.table.refresh({query: queryData});
};

$(function () {
    var defaultColunms = ExcelUpload.initColumn();
    var table = new BSTable(ExcelUpload.id, "/excelUpload/list", defaultColunms);
    table.setPaginationType("client");
    ExcelUpload.table = table.init();

    //初始化上传控件
    var avatarUp = new $WebFileUpload("excelFile","/excelUpload/upload");
    avatarUp.setUploadBarId("progressBar");
    avatarUp.init();
});
